/**
 * @Copyright 
 * @Project inetdisk
 * @CreateTime 2015-10-29 下午1:20:33
 */
package ren.wxyz.inetdisk.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import ren.wxyz.inetdisk.service.FileService;
import ren.wxyz.inetdisk.vo.RespMsg;

/**
 * <p>
 * 文件的上传和下载
 * </p>
 * @Author wxyz
 * @CreateTime 2015-10-29 下午1:20:33 init class
 */
@Controller
@RequestMapping("/file")
public class FileController {
	
	@Autowired
	private FileService fileService;
	
	/**
	 * <p>
	 * 下载文件
	 * </p>
	 * @param fileId 文件的id
	 * @param req
	 * @return
	 * @throws IOException
	 * @CreateTime 2015-10-29 下午4:08:12 create method
	 */
	@RequestMapping(value = "/{fileId}")
	public void download(@PathVariable("fileId") String fileId, HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("fileId=" + fileId);
		fileId = "使用手册_讯飞联邦数据交换(DE).docx";
		
		String fileName = new String(fileId.getBytes("GBK"),"iso-8859-1");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/octet-stream;charset=UTF-8");
		resp.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
		
		fileService.download(resp.getOutputStream(), fileId);
	}
	
	/**
	 * <p>
	 * 获取单个文件的信息，目录的话，则返回目录下的所有文件
	 * </p>
	 * @param fileId 文件id
	 * @param req
	 * @return
	 * @CreateTime 2015-10-29 下午4:51:13 create method
	 */
	@RequestMapping("/info/{fileId}")
	@ResponseBody
	public RespMsg<String> info(@PathVariable("fileId") String fileId, HttpServletRequest req) {
		RespMsg<String> msg = new RespMsg<>(1, "数据正常！");
		System.out.println("info,fileid="+fileId);
		return msg;
	}
	
	/**
	 * <p>
	 * 上传文件
	 * </p>
	 * @param files 文件列表
	 * @param req
	 * @return
	 * @CreateTime 2015-10-29 下午4:54:51 create method
	 */
	@RequestMapping(value = "/upload")
	@ResponseBody
	public RespMsg<String> upload(MultipartFile[] files,HttpServletRequest req) {
		RespMsg<String> msg = new RespMsg<>(1, "已经开始上传！");
		System.out.println("upload");
		return msg;
	}
	
	
	/**
	 * <p>
	 * 查询上传文件的进度
	 * </p>
	 * @param uuid 上传文件返回的唯一id
	 * @param req
	 * @return
	 * @CreateTime 2015-10-29 下午4:55:15 create method
	 */
	@RequestMapping(value = "/upload/progress/{uuid}")
	@ResponseBody
	public RespMsg<String> uploadProgress(@PathVariable("uuid") String uuid,HttpServletRequest req) {
		RespMsg<String> msg = new RespMsg<>(1, "已经完成10%了");
		System.out.println("uploadProgress,uuid=" + uuid);
		return msg;
	}
}
